﻿Imports Abyx.SVCMS.Library.DB
Imports Abyx.SVCMS.Library

Public Class AppSettings
    Public Shared LoggedUser As UserType = Nothing
    Public Shared Dont_Load_Updates_At_Startup As Boolean = False
    Public Shared Settings As SettingsType = Nothing
    Public Shared MainForm As SVCM_Form = Nothing

    Public Const DatePicker_MinDate As DateTime = #1/1/1753#


#Region "Manage Settings"
    Public Shared Sub LoadSettings()
        Dim sysman As New SysManager(AppSettings.LoggedUser)
        AppSettings.Settings = sysman.Settings_Read
    End Sub
    Public Shared Sub SaveSettings()
        If AppSettings.Settings IsNot Nothing Then
            Dim sysman As New SysManager(AppSettings.LoggedUser)
            sysman.Settings_Save(AppSettings.Settings)
        End If
    End Sub
    Public Shared Sub PushFormSettingsValue(form As Form, _
                                     controlName As String, _
                                     propertyName As String, _
                                     value As Object)
        Dim s As New FormSettingsType(form.Name, controlName, propertyName, value)
        If AppSettings.Settings IsNot Nothing Then
            Dim index As Integer = AppSettings.Settings.FormSettings.IndexOf(s)
            If index > -1 Then
                Dim ss As FormSettingsType = AppSettings.Settings.FormSettings(index)
                ss.Value = value
            Else
                AppSettings.Settings.FormSettings.Add(s)
            End If
        End If
    End Sub
    Public Shared Sub PushFormSettingsValue(form As Form, _
                                     control As Control, _
                                     propertyName As String, _
                                     value As Object)
        Dim s As New FormSettingsType(form.Name, control.Name, propertyName, value)
        If AppSettings.Settings IsNot Nothing Then
            Dim index As Integer = AppSettings.Settings.FormSettings.IndexOf(s)
            If index > -1 Then
                Dim ss As FormSettingsType = AppSettings.Settings.FormSettings(index)
                ss.Value = value
            Else
                AppSettings.Settings.FormSettings.Add(s)
            End If
        End If
    End Sub
    Public Shared Sub InitFormSettings(ByRef form As Form, Optional WriteSettings As Boolean = False)
        If WriteSettings Then
            'form state / size
            If form.WindowState <> FormWindowState.Maximized Then
                AppSettings.PushFormSettingsValue(form, form, "Size", form.Size)
                AppSettings.PushFormSettingsValue(form, form, "Location", form.Location)
            End If
            AppSettings.PushFormSettingsValue(form, form, "WindowState", form.WindowState)
        Else
            'form size
            If AppSettings.Settings.FormSettings.Item(form, form, "Size") IsNot Nothing Then
                form.Size = AppSettings.Settings.FormSettings.Item(form, form, "Size").Value
            End If
            'form location
            If AppSettings.Settings.FormSettings.Item(form, form, "Location") IsNot Nothing Then
                form.Location = AppSettings.Settings.FormSettings.Item(form, form, "Location").Value
            End If
            'form state
            If AppSettings.Settings.FormSettings.Item(form, form, "WindowState") IsNot Nothing Then
                form.WindowState = AppSettings.Settings.FormSettings.Item(form, form, "WindowState").Value
            End If
        End If
    End Sub
#End Region

#Region "Window Management"
    Public Shared Function GetFormInstanceForDataObject(DataObject As Object) As System.Windows.Forms.Form
        Return MainForm.GetFormInstanceForDataObject(DataObject)
    End Function
#End Region

End Class

Public Enum AccessRightsEnum
    SEC_CreateNewUser
    SEC_DeleteUser

End Enum